package com.yuerlive.paperplane.module.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yuerlive.paperplane.R;
import com.yuerlive.paperplane.data.source.local.ZhihuDailyNewsLocalDataSource;
import com.yuerlive.paperplane.data.source.remote.ZhihuDailyNewsRemoteDataSoure;
import com.yuerlive.paperplane.data.source.repository.ZhihuDailyNewsRepository;
import com.yuerlive.paperplane.timeline.DoubanMomentFragment;
import com.yuerlive.paperplane.timeline.GuokrHandpickFragment;
import com.yuerlive.paperplane.timeline.TimelineFragmentPagerAdapter;
import com.yuerlive.paperplane.timeline.ZhihuDailyFragment;
import com.yuerlive.paperplane.timeline.ZhihuDailyPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Admin on 2017/7/3.
 */

public class TimeLineFragment extends Fragment {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    private ZhihuDailyFragment mZhihuFragment;
    private DoubanMomentFragment mDoubanFragment;
    private GuokrHandpickFragment mGuokrFragment;

    public static TimeLineFragment newInstance() {
        return new TimeLineFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState!=null){
            FragmentManager fm=getChildFragmentManager();
            mZhihuFragment= (ZhihuDailyFragment) fm.getFragment(savedInstanceState,ZhihuDailyFragment.class.getSimpleName());
            mDoubanFragment= (DoubanMomentFragment) fm.getFragment(savedInstanceState,DoubanMomentFragment.class.getSimpleName());
            mGuokrFragment= (GuokrHandpickFragment) fm.getFragment(savedInstanceState,GuokrHandpickFragment.class.getSimpleName());
        }else{
            mZhihuFragment=ZhihuDailyFragment.newInstance();
            mDoubanFragment=DoubanMomentFragment.newInstance();
            mGuokrFragment=GuokrHandpickFragment.newInstance();
        }
      new ZhihuDailyPresenter(mZhihuFragment, ZhihuDailyNewsRepository.getInstance(ZhihuDailyNewsRemoteDataSoure.getInstance(), ZhihuDailyNewsLocalDataSource.getInstance(getContext())));
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        FragmentManager fm =getChildFragmentManager();
        if(mZhihuFragment.isAdded()){
            fm.putFragment(outState,ZhihuDailyFragment.class.getSimpleName(),mZhihuFragment);
        }
        if(mDoubanFragment.isAdded()){
            fm.putFragment(outState,DoubanMomentFragment.class.getSimpleName(),mDoubanFragment);
        }
        if(mGuokrFragment.isAdded()){
            fm.putFragment(outState,GuokrHandpickFragment.class.getSimpleName(),mGuokrFragment);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_timeline, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       viewPager.setAdapter(new TimelineFragmentPagerAdapter(getChildFragmentManager(),getContext(),mZhihuFragment,
               mDoubanFragment,mGuokrFragment));
        viewPager.setOffscreenPageLimit(3);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
               if(tab.getPosition()==2){
                   fab.hide();
               } else{
                   fab.show();
               }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
